package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.exception.ServiceException;
import cn.darkhorse.ticket.basis.mapper.TicketProcedureMapper;
import cn.darkhorse.ticket.basis.pojo.TicketProcedure;
import cn.darkhorse.ticket.basis.pojo.TicketRouting;
import cn.darkhorse.ticket.basis.service.TicketProcedureService;
import cn.darkhorse.ticket.basis.service.TicketRoutingService;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.production.pojo.TicketTask;
import cn.darkhorse.ticket.production.service.TicketInfoService;
import cn.darkhorse.ticket.production.service.TicketTaskService;
import cn.darkhorse.ticket.service.IRepeatCheckService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 工序 服务实现类
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@Service
public class TicketProcedureServiceImpl extends ServiceImpl<TicketProcedureMapper, TicketProcedure> implements TicketProcedureService {
	
	@Resource
	private TicketRoutingService routingService;
	@Resource
	private IRepeatCheckService<TicketProcedure> repeatCheckService;
	
	@Override
	public String repeat(TicketProcedure procedure) {
		String code = null;
		if(null==procedure.getId()){
			code = repeatCheckService.repeatAutoCodeAndName(this, procedure.getId(), TicketConstants.BUSINESS_TYPE_GX,
					procedure.getCode(), procedure.getName(), procedure.getIsAutomatically());
		}else {
			repeatCheckService.repeatName(this,procedure.getId(),procedure.getName());
		}
		return code;
	}
	@Resource
	private TicketTaskService taskService;
	
	
	@Override
	public boolean isQuote(Long id) {
		TicketProcedure procedure = getById(id);
		if(null == procedure){
			throw new ServiceException("工序不存在！");
		}
		long count = routingService.count(new LambdaQueryWrapper<TicketRouting>()
				.eq(TicketRouting::getProcedureId, id));
		if(count>0){
			return false;
		}
		
		LambdaQueryWrapper<TicketTask> taskWrapper = new LambdaQueryWrapper<>();
		taskWrapper.eq(TicketTask::getProcedureCode,procedure.getCode());
		long count2 = taskService.count(taskWrapper);
		if(count2>0){
			return false;
		}
		return true;
	}
	
	
}
